#include"SeqList.h"
void SLPrint(SL* ps)
{
	assert(ps);
	int i = 0;
	for ( i = 0; i < ps->size;++i)
	{
		printf("%d", ps->a[i]);
	}
	printf("\n");
}
void SLDestory(SL* ps)
{
	assert(ps);
	if (ps->a)
	{
		free(ps->a);
		ps->a = NULL;
		ps->size = ps->capacity = 0;
	}

}
void SLInit(SL* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}
void SLPushBack(SL* ps, SLDateType x)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDateType* tmp = (SLDateType*)realloc(ps->a, newcapacity*sizeof(SLDateType));

		if (tmp == NULL)
		{
			perror("realloc faulse");
			exit(-1);
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->size] = x;
	ps->size++;
}
void SLPopBack(SL *ps)
{
	assert(ps);
	assert(ps->size > 0);
	
	ps->size--;

}
void SLCheckCapacity(SL* ps)
{
	assert(ps);
	if (ps->capacity == ps->size)
	{
		int newcapacity = 0;
		 newcapacity = ps->capacity == 0 ? 4 : newcapacity * 2;
		SLDateType* tmp = (SLDateType*)realloc(ps, sizeof(SLDateType) * newcapacity);
		if (NULL == tmp)
		{
			perror("realloc fail");
			exit(-1);
		}
		ps->capacity = newcapacity;
		ps->a = tmp;
	}
	
}
void SLPushFront(SL* ps, SLDateType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	int end = ps->size;
	while (end--)
	{
		ps->a[end+1] = ps->a[end];
	}
	ps->a[0] = x;
	ps->size++;
}
void SLPopFront(SL* ps)
{
	assert(ps);
	SLCheckCapacity(ps);

	for (int i = 0; i < ps->size-1; i++)
	{
		ps->a[i] = ps->a[i + 1];
	}
	ps->size--;
}
void SLInsert(SL* ps, int pos, SLDateType x)
{
	assert(ps);
	assert(pos >= 0);
	assert(pos <= ps->size);
	SLCheckCapacity(ps);
	int end = ps->size;
	while (end >= pos)
	{
		ps->a[end] = ps->a[end - 1];
		end--;
	}
	ps->a[pos] = x;
	ps->size++;

}
void SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0);
	assert(pos <= ps->size - 1);
	while (pos < ps->size - 1)
	{
		ps->a[pos] = ps->a[pos + 1];
			pos++;
	}
	ps->size--;
}
int SLFind(SL* ps, SLDateType x, int begin)
{
	assert(ps);
	for (int i = begin; i < ps->size; i++)
	{
		if (x == ps->a[i])
		{
			return i;
		}
	}
	return -1;
}
